unit utils;
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
interface
uses Classes, e_log;
type Pstringlist = ^TstringList;

procedure  _error(const errmsg: string);
procedure Save_FIle(var fl : TstringList; const filter: string);
procedure Unzip(const from,dest: string);

function GetTmpDir : string;
function Open_FIle(const filter: string): string;
function Open_Mfile(const filter: string): PstringList;
function Open_FOLDER: string;
function Str_ToLow(const ext: string): string;
function Get_files(const path: string; const filter: string): PStringList;
function GetEnvVarValue(const VarName: string): string;
function check_extension(const file_path: string): boolean;


function strPwide(const Wide: PWideChar): WideString;
function ANSI_WIDE(const str: AnsiString): PwideChar;
function WIDE_ANSI(const str: Widestring): PAnsiChar;

implementation
uses
  SysUtils, Dialogs, unit1, fileCtrl, pl_consts, visuals, trans_ru, zlibfunc {$IFNDEF LINUX} ,windows{$ENDIF};


function GetEnvVarValue(const VarName: string): string;
  {Returns the value for the given environment variable or '' if the variable
  does not exist}
var
  BufSize: Integer;  // buffer size required for value (including terminal #0)
begin  {$IFNDEF LINUX}
  // Get required buffer size (includes space for terminal #0)
  BufSize := GetEnvironmentVariable(PChar(VarName), nil, 0);
  // Read env var value into result string
  SetLength(Result, BufSize - 1); // space for #0 automatically added
  GetEnvironmentVariable(PChar(VarName), PChar(Result), BufSize);
  {$ELSE}
    GetEnvVarValue := '/tmp';
  {$ENDIF}
end;

procedure Unzip(const from, dest: string);
begin
  zlibfunc.DecompressFile(from, dest, true, false);
end;
{----ANSI-UTF-CONVERSION------------------}
function StrPwide(const Wide: PwideChar): Widestring;
begin
  StrPwide := Wide;
end;

function ANSI_WIDE(const str: AnsiString): PwideChar;
var
  wstr: widestring;
begin
  wstr := str;
  ANSI_WIDE := @wstr;
end;

function WIDE_ANSI(const str: Widestring): PAnsiChar;
var
  ans: Ansistring;
begin
  ans := str;
  WIDE_ANSI := @ans;
end;


function Tolow(const ch: char): char;
begin
  Tolow := ch;
  if (ch in ['A' .. 'Z']) then
    Tolow := chr(ord(ch)+ ord('a') - ord('A'));
end;

function GetTmpDir : string;
var
  res: string;
  i: Byte;
begin
  res := GetEnvVarValue('TEMP');
  res := res + '\zlib';
  for i := 1 to 5 do
    res := res + inttostr(random(10));
  mkdir(res);
  GettmpDir := res;
end;

function Str_ToLow(const ext: string): string;
var
  i: Cardinal;
  res: string;
begin
  res := ExtractFileExt(ext);
  if (res <> '') then Delete(res, 1, 1);
  for i := 1 to length(res) do
    res[i] := ToLow(res[i]);
  Str_ToLow := res;
end;

function check_extension(const file_path: string): boolean;
var
  ext: string;
  i: Cardinal;
begin
  check_extension := false;
  ext := ExtractFileExt(file_path);
  ext := Str_ToLow(ext);
  {i := 0;
  while ((i < sp_exts.Count)and(sp_exts.Strings[i] <> ext)) do inc(i);
  if (i <> sp_exts.Count) then
    check_extension := true;   }
  check_extension := spexts_Find(ext);
end;

function Get_FILEs(const path: string; const filter: string): PstringList;
var
  sr: TsearchRec;
  file_list: TstringList;
  dir: string;
begin
  dir := path;
  file_list := TstringList.Create;
  if (FindFirst(path +_slash + filter,(faAnyFile) and (not faDirectory), sr) = 0) then
  begin
    if (check_extension(sr.Name)) then
      file_list.Append(path + _slash + sr.Name);
    while FindNext(sr)= 0 do
      if (check_extension(sr.Name)) then
        file_list.Append(path + _slash + sr.Name);
  end;
  get_FILes := @file_list;
end;

function Open_Folder: string;
var
  res: String;
begin
  SelectDirectory(_TITLE_DIRECTORY_SELECT,'', res);
  Open_Folder := res;
end;

function Open_MFIle(const filter: string): PstringList;
var
  Tfile: TopenDialog;
  file_list: TstringList;
  i: Longint;
begin
  file_list := TstringList.Create;
  Tfile := TopenDialog.Create(nil);
  Tfile.Title := _TITLE_FILE_SELECT;
  Tfile.Filter:= filter;
  Tfile.FilterIndex:= 1;
  Tfile.Options := [ofFileMustexist,  ofAllowMultiSelect];
  Open_MFILE := nil;
  if Tfile.Execute then
  begin
    for i := 0 to Tfile.Files.Count - 1 do
      file_list.Append(Tfile.Files[i]);
    Open_MFile := @File_List;
  end;
  //Tfile.FreeInstance;
end;

function Open_FIle(const filter: string): string;
var
  Tfile: TopenDialog;
begin
  Tfile := TopenDialog.Create(nil);
  Tfile.Filter:= filter;
  Tfile.FilterIndex:= 1;
  Tfile.Options := [ofFileMustexist];
  Open_FILE := '';
  if Tfile.Execute then Open_FILE := Tfile.filename;
  Tfile.FreeInstance;
end;


procedure Save_FIle(var fl : TstringList; const filter: string );
var
  Tfile: TsaveDialog;
begin
  Tfile := TsaveDialog.Create(nil);
  Tfile.Filter:= filter;
  if (Tfile.Execute) then fl.savetoFile(tfile.FileName);
  Tfile.FreeInstance;
end;

procedure _Error(const errmsg: string);
begin
  //MessageDlg(errmsg, mtWarning, [mbOk], 0)
 // e_log.Form2.log_memo.Read_Onl
  e_log.Form2.log_memo.Lines.Append(errmsg);
  e_log.Form2.Show;
end;

end.